layui.use(['table', 'laytpl', 'layer'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var listname;
	$("#searchBtn").click(function() {
		listname = $("#listname").val();
		if(listname == "") {
			layer.msg("请输入搜索条件", {
				time: 500
			});
		} else {
			getxianshi();
		}
	});
	$("#reLoad").click(function() {
		listname = "";
		layer.msg("刷新", {
			time: 500
		}, function() {
			getxianshi();
		});
	});
	getxianshi();

	function getxianshi() {
		$.ajax({
			type: "post",
			url: "/getxianshi",
			async: true,
			data: {
				listname: listname
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_reload',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'title',
								title: '图片主题',
								width: 150,
								sort: true,
							}, {
								fixed: 'right',
								title: '操作',
								width: 300,
								align: 'center',
								toolbar: '#barDemo'
							}, {
								field: '',
								title: '',
								width: 510,
							}
						]
					],
					"data": result
				});
			}
		});
	}
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'edit') {
			detail(data.id, "edit");
		} else if(layEvent === 'detail') {
			checkDetails(data.id,"detail");
		}
	});
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '编辑图片信息',
			type: 2,
			content: '/static/msg/picture/xianshi_edit.html?id=' + id + "&type=" + type,
			area: ['300px', '320px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var img = body.find("#flowerimg")[0].src;
				var title = body.find(".imgtitle").val();
				var isgo=true;
				if(!title || title == undefined || title == "") {
					layer.msg('图片主题不能为空', {
						time: 2000
					});
					var isgo = false;
				}
				if(isgo) {
					tijiao();
				}

				function tijiao() {
					$.ajax({
						type: "post",
						url: "/xianshi/edit",
						data: {
							id: id,
							img:img,
							title:title
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage, {
								time: 1000
							}, function() {
								if(data.code == '1') {
									layer.close(index);
									getxianshi();
								}
							});
						}
					});
				}
			}
		});
	}
	//查看鲜花信息
	function checkDetails(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '鲜花详情',
			type: 2,
			content: '/static/msg/picture/xianshi_edit.html?id=' + id + "&type=" + type,
			area: ['300px', '300px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
	
});